<?php
class Model_FacultyMember extends CCMIS_Model {
    //Database faculty_member
	public function __construct(){
		parent::__construct();
                
        $this->load->database();
	}

    public function check_account($username, $password)
    {
        $accounts = $this->getAll();

        foreach($accounts as $account)
        {
            if($account['Username'] == $username)
            {
                if($account['Password'] == $password)
                {
                    return array("status" => "true", "username" => $account['Username'], "user_id" => $account['MemberID']);
                }
                else
                {
                    return array("status" => false, "message" => 'Account found; Incorrect Password.');
                }
            }
        }
        return array("status" => false, 'message' => 'Account not found');
    }

    public function getAll()
    {
        $sql = "SELECT * FROM faculty_member";
        $query =  $this->db->query($sql);
        $result = $query->result_array();
        return $result;
    }
    public function getByMemberId($MemberID)
    {
        $sql = "SELECT * FROM faculty_member WHERE MemberID LIKE ?";
        $query =  $this->db->query($sql, $MemberID);
        $result = $query->row_array();
        return $result;
    }

    public function getAllByName($name)
    {
        if($name == '')
            return array();

        $sql = "SELECT * FROM faculty_member WHERE FirstName LIKE '%".$name."%' OR MiddleName LIKE '%".$name."%' OR LastName LIKE '%".$name."%' ";
        $query =  $this->db->query($sql);
        $result = $query->result_array();
        return $result;
        //*/

    }
    public function getAllByDeptId($deptID)
    {
        $sql = "SELECT * FROM faculty_member WHERE DepartmentID LIKE ?";
        $query =  $this->db->query($sql, $deptID);
        $result = $query->result_array();
        return $result;
    }

    public function add($username, $password, $specialty, $fname,$mname,$lname, $gender,
                        $civilstatus, $citizenship, $religion, $contactno, $email, $bday ,$address, $status, $rank, $dept)
    {

        $bday = date_format(date_create($bday), 'Y-m-d');

        $sql = 'INSERT INTO `faculty_member`(
        `Username`,
        `Password`,
        `Specialty`,
        `FirstName`,
        `MiddleName`,
        `LastName`,
        `Gender`,
        `Civil_Status`,
        `Citizenship`,
        `Religion`,
        `ContactNo`,
        `Email`,
        `Birthday`,
        `Address`,
        `Status`,
        `RankID`,
        `DepartmentID`
        )
        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';

        $this->db->query($sql,
            array(
                $username, $password, $specialty, $fname,$mname,$lname, $gender,
                $civilstatus, $citizenship, $religion, $contactno, $email, $bday ,$address, $status, $rank, $dept
            ));
    }
    public function edit(
        $memID,$username, $password, $specialty, $fname,$mname,$lname, $gender,
        $civilstatus, $citizenship, $religion, $contactno, $dept, $email, $bday ,$address, $status)
    {
        $sql = "UPDATE `faculty_member`
        SET

        `Username` = ?,
        `Password` = ?,
        `Specialty` = ?,
        `FirstName` = ?,
        `MiddleName` = ?,
        `LastName` = ?,
        `Gender` = ?,
        `Civil_Status` = ?,
        `Citizenship` = ?,
        `Religion` = ?,
        `ContactNo` = ?,
        `Email` = ?,
        `Birthday` = ?,
        `Address` = ?,
        `Status` = ?
         WHERE ID = ?";

        $this->db->query($sql, array(
            $username, $password, $specialty, $fname,$mname,$lname, $gender,
            $civilstatus, $citizenship, $religion, $contactno, $dept, $email, $bday ,$address,$status,
            $memID
        ));

    }
    public function getNameAppendedByID($id)
    {
        $sql = "SELECT FirstName, MiddleName, LastName FROM faculty_member WHERE MemberID LIKE ?";
        $query =  $this->db->query($sql, $id);
        $result = $query->row_array();
        return $result['LastName'].', '.$result['FirstName'].' '.$result['MiddleName'];
    }
    public function getIdBySpecifics($fname, $mname, $lname, $bday)
    {
        $bday = date_format(date_create($bday), 'Y-m-d');

        $sql = "SELECT * FROM faculty_member
        WHERE FirstName = ? AND MiddleName = ? AND LastName = ? AND Birthday = ?";
        $query =  $this->db->query($sql, array($fname, $mname, $lname, $bday));
        $result = $query->row_array();

        return $result['MemberID'];
        //*/

    }

    public function search($name, $dept, $spec)
    {
        $sql = "SELECT * FROM faculty_member ";

        if($name != '')
        {
            $sql .= "WHERE FirstName LIKE '%".$name."%' OR MiddleName LIKE '%".$name."%' OR LastName LIKE '%".$name."%' ";
        }
        if($dept != '')
        {
            if (strpos($sql, "WHERE") !== false)
            {
                $sql .= "AND ";
            }
            else
            {
                $sql .= "WHERE ";
            }
            $sql .= "DepartmentID = ".$dept." ";
        }
        if($spec != '')
        {
            if (strpos($sql, "WHERE") !== false)
            {
                $sql .= "AND ";
            }
            else
            {
                $sql .= "WHERE ";
            }
            $sql .= "Specialty LIKE '%".$spec."%' ";
        }


        //return $sql;

        $query =  $this->db->query($sql);
        $result = $query->result_array();
        return $result;


    }


}
